package com.imooc.seataproduct;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-02-12 08:12
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductService {

    private final ProductDao productDao;

    public void deduct(Long productId, Integer count) {

        log.info("开始扣减库存，productId={}, count={}", productId, count);
        Optional<ProductEntity> byId = productDao.findById(productId);
        if (byId.isPresent()) {
            ProductEntity entity = byId.get();
            entity.setCount(entity.getCount() - count);
            productDao.save(entity);
        }
    }

}
